class socket {
    private static mqant = Lib.Mqant.getInstance();
    public static coonect(){
        var useSSL = 'https:' == document.location.protocol ? true :false ;
        const connectionOptions = {
            // host: "192.168.1.132",
            host:"h5-svc.farm.pk0818.com",
			port: 3653,
			client_id:common.userInfo.userId + "",
            useSSL:true,
            onSuccess: (): void => { 
                egret.log("onConnected");
                socket.request("socketLogin");
                Dispatcher.ins.dispatchEvent(new GameEvent("login"));
            },
            onFailure:(message) =>{ egret.log('connection failed: ' + message.errorMessage);setTimeout(socket.coonect(),1000);},
            onConnectionLost:()=>{ 
                egret.log('onConnectionLost','connecting Lost'); 
                setTimeout(socket.coonect(),1000);
            },
        }
        this.mqant.init(connectionOptions);
    }
    /**
     * config:请求接口
     * dataPost:请求数据
     * note:注释，可不传
     */
    public static request(config,dataPost:any={},note:any = -1){
        let topic = common.mqTopic[config];
        let msg = this.madeUpMsg(dataPost);
        this.mqant.request(topic,msg,(data)=>{
            let result = JSON.parse(data.payloadString);
            let obj:any = {};
            obj.list = [];
            obj.note = note;
            /**
             * 解析
             * 1.出现错误
             * 2.没有错误返回数据
             * 3.数据形式
             */
            if(result.Error){
                if(config !== "friendList"){
                    Dispatcher.ins.dispatchEvent(new GameEvent("toast",result.Error));
                }
            }else{
                for(let i in result.Result){
                    if(i !== "list"){
                        obj[i] = result.Result[i];
                    }
                }
                for(let i in result.Result.list){
                    obj.list.push(result.Result.list[i]);
                }
                Dispatcher.ins.dispatchEvent(new GameEvent(config,obj));
            }
        });
    }

    public static madeUpMsg(dataPost:any){
        let msg:any = {};
        msg.areaId = common.areaId;
        msg.userId = common.userInfo.userId;
        for(let i in dataPost){
            msg[i] = dataPost[i];
        }
        return msg;
    }
}